<?php

// ------------------------------------------------------------------------
// |@Author       : Jarmin <topextend@qq.com>
// |@----------------------------------------------------------------------
// |@Date         : 2025-05-09 18:59:40
// |@----------------------------------------------------------------------
// |@LastEditTime : 2025-05-09 20:58:44
// |@----------------------------------------------------------------------
// |@LastEditors  : Jarmin <topextend@qq.com>
// |@----------------------------------------------------------------------
// |@Description  : 系统配置分组模型
// |@----------------------------------------------------------------------
// |@FilePath     : SystemConfigGroup.php
// |@----------------------------------------------------------------------
// |@Copyright (c) 2025 http://www.ladmin.cn All Rights Reserved.
// ------------------------------------------------------------------------
declare (strict_types=1);

namespace think\admin\model;

use think\admin\Model;
use think\model\relation\HasMany;

/**
 * 系统配置分组模型
 * Class SystemConfigGroup
 * @package think\admin\model
 */
class SystemConfigGroup extends Model
{
    // 设置字段信息（根据数据库表结构补充）
    protected $schema = [
        'id'         => 'int',
        'name'       => 'string',
        'code'       => 'string',
        'sort'       => 'int',
        'remark'     => 'string',
    ];

    /**
     * 关联配置项
     * @return HasMany
     */
    public function configs(): HasMany
    {
        return $this->hasMany(SystemConfig::class, 'group_id', 'id');
    }

    /**
     * 获取配置组及其配置
     * @param int $site_id 站点ID
     * @param string|array $code 配置组编码
     * @return array
     */
    public function getWithConfigs(int $site_id = 0, $code = null): array
    {
        $query = $this->order(['sort' => 'asc']);

        // 如果code不为空，添加code条件
        if (!empty($code)) {
            $query->whereIn('code', is_array($code) ? $code : [$code]);
        }

        return $query->with(['configs' => function ($query) use ($site_id) {
            $query->where(['site_id' => $site_id])->order(['sort' => 'asc']);
        }])->select()->toArray();
    }
}
